package com.sh.studytest.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sh.studytest.listener.OnItemChildClickListener;
import com.sh.studytest.listener.OnItemChildLongClickListener;
import com.sh.studytest.listener.OnItemClickListener;
import com.sh.studytest.R;
import com.sh.studytest.listener.OnItemLongClickListener;

import java.util.List;

public class StringAdapter extends RecyclerView.Adapter<StringAdapter.ViewHolder> {



    private List<String> lists;
    private OnItemClickListener listener;
    private OnItemChildClickListener childClickListener;

    private OnItemChildLongClickListener childLongClickListener;

    private OnItemLongClickListener longClickListener;

    public StringAdapter(List<String> lists) {
        this.lists = lists;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener childClickListener) {
        this.childClickListener = childClickListener;
    }


    public void setOnItemLongClickListener(OnItemLongClickListener longClickListener){
        this.longClickListener = longClickListener;
    }


    public  void setOnItemChildLongClickListener(OnItemChildLongClickListener childLongClickListener){
        this.childLongClickListener = childLongClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text_rv, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(v -> {
            if(listener != null){
                listener.onItemClick(v, viewHolder.getAdapterPosition());
            }
        });

        //添加子控件点击事件
        view.findViewById(R.id.btn_test).setOnClickListener(v -> {
            if (childClickListener != null) {
                childClickListener.onItemChildClick(v, viewHolder.getAdapterPosition());
            }
        });

        view.findViewById(R.id.btn_test_2).setOnClickListener(v -> {
            if (childClickListener != null) {
                childClickListener.onItemChildClick(v, viewHolder.getAdapterPosition());
            }
        });



        view.setOnLongClickListener(v -> {
            if (longClickListener != null) {
                return longClickListener.onItemLongClick(v, viewHolder.getAdapterPosition());
            }
            return false;
        });

        view.findViewById(R.id.btn_test).setOnLongClickListener(v -> {
            if (childLongClickListener != null) {
                return childLongClickListener.onItemChildLongClick(v, viewHolder.getAdapterPosition());
            }
            return false;
        });


        return viewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.tvText.setText(lists.get(position));
    }


    @Override
    public int getItemCount() {
        return lists.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvText;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvText = itemView.findViewById(R.id.tv_text);
        }
    }





}
